var app = getApp(), api = getApp().api; 

Page({
    data: {
        isPageShow: !1,
        order: null,
        share_modal_active:null,//是否打开分享弹窗
        user_id:null,//当前用户的user_id
        share_user_id:0,//分享用户的user_id
        show_pay_status:false,// 付款按钮是否显示
        pay_type_list:[],
        order_list:[],//同列表调用一个支付的时候使用
        getGoodsTotalPrice: function() {
            return this.data.order.total_price;
        },
        hide: 1,
        qrcode: "",
    },
    onLoad: function(e) {
        getApp().page.onLoad(this, e);
        var t = this;
        getApp().core.showLoading({
            title: "正在加载"
        });
        var o = getCurrentPages(), a = o[o.length - 2];
        t.setData({
            user_id :getApp().core.getStorageSync("USER_INFO").id,
            share_user_id: e.user_id && e.user_id != 0 ? e.user_id : 0,
        });
        if(t.data.pay_type_list.length <= 0){
            //获取可选择的支付方式
            getApp().request({
                url:getApp().api.order.pay_type_list,
                success:function(res){
                    if(res.code == 0){
                        t.setData({
                            pay_type_list:res.data
                        })
                    }
                }
            })
        }
        
        getApp().request({
            url: getApp().api.order.detail,
            data: {
              order_id: e.id,
              user_id:t.data.share_user_id,
              route: a && a.route ? a.route:''
            },
            success: function(e) {
                console.log();
                t.data.share_user_id > 0 && t.data.user_id != t.data.share_user_id && e.data.is_pay == 0 ? t.setData({show_pay_status:true}) : '';
                0 == e.code && t.setData({
                    order: e.data,
                    isPageShow: !0,
                    order_list:[e.data]
                });
            },
            complete: function() {
                getApp().core.hideLoading();
                t.handleShare();//验证绑定关系
            }
        });
    },
    //用户代付绑定用户为上下级关系
    handleShare:function(){
        var t = this;
        // 去绑定上下级关系
        if(t.data.user_id == t.data.share_user_id){
            return true;
        }  
        // 去执行绑定关系
        getApp().request({
            url:getApp().api.share.help_order,
            data:{
                user_id:t.data.user_id,
                share_user_id:t.data.share_user_id
            }
        })
    },
    // 弹出分享给朋友
    helpPay:function(e){
        var order_id = e.currentTarget.dataset.id;
        console.log(order_id);
        this.setData({
            share_modal_active: "active",
        });
    },
    // 关闭分享弹窗
    shareModalClose: function() {
        this.setData({
            share_modal_active: "",
        });
    },
    onShareAppMessage: function() {
        var t = this;
        return {
            title:'有一笔订单请你帮我付！',
            path:'/user/order-detail/order-detail?id=' + t.data.order.order_id + '&user_id=' + t.data.user_id,
            imageUrl:'',
        };
    },
    copyText: function(e) {
        var t = e.currentTarget.dataset.text;
        getApp().core.setClipboardData({
            data: t,
            success: function() {
                getApp().core.showToast({
                    title: "已复制"
                });
            }
        });
    },
    location: function() {
        var e = this.data.order.shop;
        getApp().core.openLocation({
            latitude: parseFloat(e.latitude),
            longitude: parseFloat(e.longitude),
            address: e.address,
            name: e.name
        });
    },
    orderRevoke: function(t) {
        var o = this;
        getApp().core.showModal({
            title: "提示",
            content: "是否退款该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function(e) {
                if (e.cancel) return !0;
                e.confirm && (getApp().core.showLoading({
                    title: "操作中"
                }), getApp().request({
                    url: getApp().api.order.revoke,
                    data: {
                        order_id: t.currentTarget.dataset.id
                    },
                    success: function(e) {
                        getApp().core.hideLoading(), getApp().core.showModal({
                            title: "提示",
                            content: e.msg,
                            showCancel: !1,
                            success: function(e) {
                                e.confirm && o.onLoad({
                                    id: o.data.order.order_id
                                });
                            }
                        });
                    }
                }));
            }
        });
    },
    orderQrcode: function(t) {
        var e = this;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), e.data.order.offline_qrcode ? (e.setData({
            hide: 0,
            qrcode: e.data.order.offline_qrcode
        }), getApp().core.hideLoading()) : getApp().request({
            url: getApp().api.order.get_qrcode,
            data: {
                order_no: e.data.order.order_no
            },
            success: function(t) {
                0 == t.code ? e.setData({
                    hide: 0,
                    qrcode: t.data.url
                }) : getApp().core.showModal({
                    title: "提示",
                    content: t.msg
                });
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    hide: function(t) {
        this.setData({
            hide: 1
        });
    },
    orderPay_1: function(e) {
        var a = this, t = a.data.pay_type_list;
        1 == t.length ? (getApp().core.showLoading({
            title: "正在提交",
            mask: !0
        }), 0 == t[0].payment && a.WechatPay(e), 3 == t[0].payment && a.BalancePay(e)) : getApp().core.showModal({
            title: "提示",
            content: "选择支付方式",
            cancelText: "余额支付",
            confirmText: "线上支付",
            success: function(t) {
                
                getApp().core.showLoading({
                    title: "正在提交",
                    mask: !0
                }), t.confirm ? a.WechatPay(e) : t.cancel && a.BalancePay(e);
            }
        });
    },
    WechatPay: function(t) {
        console.log(t)
        getApp().request({
            url: getApp().api.order.pay_data,
            data: {
                order_id: t.currentTarget.dataset.id,
                pay_type: "WECHAT_PAY"
            },
            complete: function() {
                getApp().core.hideLoading();
            },
            success: function(t) {
                0 == t.code && getApp().core.requestPayment({
                    _res: t,
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function(t) {},
                    fail: function(t) {},
                    complete: function(t) {
                        "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? getApp().core.redirectTo({
                            url: "/user/order/order?status=1"
                        }) : getApp().core.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(t) {
                                t.confirm && getApp().core.redirectTo({
                                    url: "/user/order/order?status=0"
                                });
                            }
                        });
                    }
                }), 1 == t.code && getApp().core.showToast({
                    title: t.msg,
                    image: "/images/icon-warning.png"
                });
            }
        });
    },
    BalancePay: function(t) {
        var a = this;
        getApp().request({
            url: getApp().api.order.pay_data,
            data: {
                order_id: a.data.order_id,
                pay_type: "BALANCE_PAY"
            },
            complete: function() {
                getApp().core.hideLoading();
            },
            success: function(t) {
                0 == t.code && getApp().core.redirectTo({
                    url: "/user/order/order?status=1"
                }), 1 == t.code && getApp().core.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1
                });
            }
        });
    },
    onShow: function() {
        getApp().page.onShow(this);
    },
    goHome:function(){
        wx.reLaunch({
            url: '/pages/index/index'
        })
    }
});